import Vue from 'vue'
import AgentApply from './AgentApply'

const instance = new Vue({
  data() {
    const token = this.getQueryString('token')
    const areaCode = this.getQueryString('areaCode')
    const config = {
      agentSystem: 'https://c-c-a.jiahuagame.com/game-lobby/',
    }
    if (window.location.hostname.match(/10.10.10/)) {
      // config.pre = 'https://10.10.10.107:8080/game-lobby'
      config.agentSystem = 'https://lefantian.jiahuagame.com/game-lobby'
    } else if (window.location.hostname.match(/lefantian/)) {
      config.agentSystem = 'https://lefantian.jiahuagame.com/game-lobby'
    } else {
      config.agentSystem = 'https://c-c-a.jiahuagame.com/game-lobby'
    }
    const api = {
      querySelf() {
        const url = `${config.agentSystem}/agent/apply/${areaCode}/inviter`;
        const opt = {
          method: 'GET',
          headers: {
            Authorization: token,
            'Content-type': 'application/json'
          }
        }
        return fetch(url, opt)
      }
    }
    return {
      api,
      isAgentAlready: false,
      loaded: false
    }
  },
  methods: {
    getQueryString(name) {
      var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
      var r = window.location.search.substr(1).match(reg);
      if (r != null) {
        return unescape(r[2]);
      }
      return null;
    },
    agentDone() {
      this.isAgentAlready = true
    }
  },
  mounted() {
    this.api.querySelf().then(res => res.json()).then(data => {
      console.log('mySelf', data)
      this.loaded = true
      this.isAgentAlready = data.result === 0
    })
  },
  components: { AgentApply }
})

instance.$mount('#root')